//
//  JQFYGroupListViewController.m
//  JQFY
//
//  Created by apple on 2017/8/28.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYGroupListViewController.h"
#import "JQFYGroupListTableViewCell.h"
#import "JQFYCompleteGroupListModel.h"

typedef void(^GroupListData)(void);
@interface JQFYGroupListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *groupListTb;
@property (nonatomic ,copy) NSArray<JQFYCompleteGroupListModel *> *models;
@property (nonatomic ,copy) NSMutableArray *images;
@property (nonatomic,assign) NSUInteger pageNumber;
@end
static NSString *identifier = @"ID";
@implementation JQFYGroupListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupTab];
    [self getModel:^{
        [MBProgressHUD showWithMessage:@"加载成功"];
    } failure:^{
        [MBProgressHUD showWithMessage:@"加载失败"];
    }];
    [self refresh];
}

-(void)setupUI
{
    UIImage* img = [UIImage imageNamed:@"baseBG"];
    self.view.layer.contents = (id)img.CGImage;
}
//setup tab
-(void)setupTab{
    _groupListTb = [[UITableView alloc] init];
    [self.view addSubview:_groupListTb];
    [_groupListTb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    _groupListTb.delegate = self;
    _groupListTb.dataSource = self;
    [_groupListTb registerClass:[JQFYGroupListTableViewCell class] forCellReuseIdentifier:identifier];
    _groupListTb.showsVerticalScrollIndicator = NO;
    _groupListTb.separatorStyle = UITableViewCellSeparatorStyleNone;
    _groupListTb.contentInset = UIEdgeInsetsMake(0, 0, 67, 0);
    UIImage* img = [UIImage imageNamed:@"baseBG"];
    _groupListTb.layer.contents = (id)img.CGImage;

    //设置组头
    UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"groupListHeader"]];
    _groupListTb.tableHeaderView = headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [JQFYGroupListTableViewCell getCellHeight];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15 *SCAL;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JQFYGroupListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.model = self.models[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//请求数据
-(void)getModel:(GroupListData)sussce failure:(GroupListData)faliure {
    [[JQFYNetworkTool sharedManager] request:RequestMethodPost urlString:@"http://app.uninhibited.cn/api/easemob/all/group/list" parameters:@{@"token":[JQFYUserData userDataInSandbox].token} finished:^(id responseObject, NSError *error) {
        if (error) {
            faliure();
        } else {
            sussce();
            NSArray *listArray = responseObject;
            if (listArray && [listArray isKindOfClass:[NSArray class]]) {
                _models = [NSArray yy_modelArrayWithClass:[JQFYCompleteGroupListModel class] json:listArray];
            }
            [self.groupListTb reloadData];
        }
    }];
}
//设置刷新
-(void)setRefresh{
    //下拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [header setImages:self.images forState:MJRefreshStateIdle];
    [header setImages:self.images forState:MJRefreshStatePulling];
    [header setImages:self.images forState:MJRefreshStateRefreshing];
    self.groupListTb.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    //上拉加载
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    [footer setImages:self.images forState:MJRefreshStateRefreshing];
    self.groupListTb.mj_footer = footer;
    footer.refreshingTitleHidden = YES;
    [footer setTitle:@"上拉加载更多" forState:MJRefreshStateIdle];
}
//下拉刷新
-(void)refresh{
    [self.groupListTb.mj_header beginRefreshing];

    [self getModel:^{
        [MBProgressHUD showWithMessage:@"刷新成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.groupListTb.mj_header endRefreshing];
            [self.groupListTb reloadData];
        });
    } failure:^{
        [MBProgressHUD showWithMessage:@"刷新失败"];
    }];
}
//上拉加载
-(void)loadMore{
    [self.groupListTb.mj_footer beginRefreshing];
    [self getModel:^{
        [MBProgressHUD showWithMessage:@"刷新成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.groupListTb.mj_footer endRefreshing];
            [self.groupListTb reloadData];
        });
        
        self.pageNumber += 5;
        if (self.models.count == 0) {
            return;
        }
        if (self.pageNumber >= self.models.count) {
            self.pageNumber = self.models.count;
            self.groupListTb.mj_footer.hidden = YES;
        } else {
            self.groupListTb.mj_footer.hidden = YES;
        }
        [self.groupListTb reloadData];
        [self.groupListTb.mj_footer endRefreshing];
    
     } failure:^{
         [MBProgressHUD showWithMessage:@"刷新失败"];
     }];
    
}
//懒加载
- (NSMutableArray *)images
{
    if (_images == nil) {
        _images = [NSMutableArray new];
        for (int i = 19; i < 24; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"图层%zd",i]];
            [_images addObject:image];
        }
    }
    return _images;
}



@end
